package com.xj.opcua.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.xj.opcua.client.Client;
import com.xj.opcua.client.util.OpcUaClentUtils;
import com.xj.opcua.util.StringUtils;

@RestController
@RequestMapping("/test")
public class TestController {
	
	@RequestMapping(value = "/connect" ,method = RequestMethod.GET)
	public Object connect(String url) {
		return OpcUaClentUtils.connectOpcUaClent(url);
	}
	
	@RequestMapping(value = "/close" ,method = RequestMethod.GET)
	public Object close(String url) {
		return OpcUaClentUtils.closeOpcUaClent(url);
	}
	
	@RequestMapping(value = "/sub" ,method = RequestMethod.GET)
	public Object sub(String url,String subNode,int namespaceIndex) {
		OpcUaClient client = (OpcUaClient) Client.map.get(url).get(0);
		NodeId nodeId = new NodeId(namespaceIndex, subNode);
		List<NodeId> listNode = OpcUaClentUtils.browseNodeId("", client, nodeId, namespaceIndex);
		OpcUaClentUtils.createSubscription(url, client, listNode, 500.0);
		return "";
	}
	
	@RequestMapping(value = "/getSubValues" ,method = RequestMethod.GET)
	public Object getSubValues() {
		Map<String,JSONObject> map= new LinkedHashMap<String, JSONObject>();
		map.putAll(Client.subValues);
		Client.subValues = new LinkedHashMap<String, JSONObject>();;
		return map;
	}
	
	@RequestMapping(value = "/browseNode" ,method = RequestMethod.GET)
	public Object browseNode(String url,int namespaceIndex,String identifier) {
		OpcUaClient opcUaClient = OpcUaClentUtils.getOpcUaClent(url);
		if(opcUaClient!=null) {
			List<Object[]> list = new ArrayList<Object[]>();
			NodeId nodeId = new NodeId(namespaceIndex, identifier);
	        // 开始浏览节点
			list = OpcUaClentUtils.browseNode("", opcUaClient, nodeId,list,0);
			return list;
		}
		return null;
	}
	
	@RequestMapping(value = "/read" ,method = RequestMethod.GET)
	public Object read(String url,int namespaceIndex,String identifier) {
		OpcUaClient opcUaClient = OpcUaClentUtils.getOpcUaClent(url);
		if(opcUaClient!=null) {
			NodeId nodeId = new NodeId(namespaceIndex,identifier);
	        // 开始浏览节点
			Object value = OpcUaClentUtils.readValue(opcUaClient, nodeId);
			return value;
		}
		return null;
	}
	
	@RequestMapping(value = "/write" ,method = RequestMethod.GET)
	public Object write(String url,int namespaceIndex,String identifier) {
		OpcUaClient opcUaClient = OpcUaClentUtils.getOpcUaClent(url);
		if(opcUaClient!=null) {
			boolean write = false;
			NodeId nodeId = new NodeId(namespaceIndex, identifier);
			Object value = OpcUaClentUtils.readValue(opcUaClient, nodeId);
			if("false".equalsIgnoreCase(StringUtils.toString(value))) {
				write = true;
			}
			Map<String, Object> map = new LinkedHashMap<String, Object>();
			map.put(identifier, write);
			return OpcUaClentUtils.writeValue(opcUaClient, map,namespaceIndex);
		}
		return null;
	}

}
